import axios, { AxiosResponse } from 'axios'
import { ElMessage } from 'element-plus'

// config
// const BASE_URL = "http://localhost:8080"
export const BASE_URL_USER = 'http://localhost:8001'
export const BASE_URL = 'http://localhost:8002'
export const BASE_URL_AUXILIARY = 'http://localhost:8001'
export const BASE_URL_INFRASTRUCTURE = 'http://localhost:8004' //基建项目url
export const BASE_URL_GENERIC = 'http://localhost:8005' //装备购置项目url
export const BASE_URL_UPDATE = 'http://localhost:8006' //装备购置项目url
export const BASE_URL_REPAIR = 'http://localhost:8007'
export const BASE_URL_EQUIPMENT = 'http://localhost:8001'
export const TIMEOUT = 60000

// 创建axios实例
const request = axios.create({
	//一般项目
	baseURL: BASE_URL, // api 的 base_url  development   production
	timeout: TIMEOUT, // 请求超时时间
	withCredentials: true
})
const auxiliary = axios.create({
	baseURL: BASE_URL_AUXILIARY, // api 的 base_url  development   production
	timeout: TIMEOUT, // 请求超时时间
	withCredentials: true
})
const user = axios.create({
	baseURL: BASE_URL_USER, // api 的 base_url  development   production
	timeout: TIMEOUT, // 请求超时时间
	withCredentials: true
})
const infrastructure = axios.create({
	baseURL: BASE_URL_INFRASTRUCTURE,
	timeout: TIMEOUT,
	withCredentials: true
})
const generic = axios.create({
	baseURL: BASE_URL_GENERIC,
	timeout: TIMEOUT,
	withCredentials: true
})
const updateRequest = axios.create({
	baseURL: BASE_URL_UPDATE,
	timeout: TIMEOUT,
	withCredentials: true
})

const purchaseRequest = axios.create({
	baseURL: BASE_URL_EQUIPMENT,
	timeout: TIMEOUT,
	withCredentials: true
})

const repairRequest = axios.create({
	baseURL: BASE_URL_REPAIR,
	timeout: TIMEOUT,
	withCredentials: true
})

// request拦截器
/* request.interceptors.request.use(
    config => {
      config.headers['token'] = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIyIiwiaW5mbyI6eyJ0eXBlIjoxLCJ1c2VySWQiOjIsImVtYWlsIjoiMTIzQDEyMy5jb20iLCJ1c2VybmFtZSI6InRlc3QifX0.xf2XZi4QNPdBgIYwghZYeqO6PRE_YkpEgfJ_nLF6GAs' // 让每个请求携带自定义token 请根据实际情况自行修改
      config.headers['Content-Type'] = 'application/json'
      return config
    },
    error => {
      Promise.reject(error)
    }
  ) */

request.interceptors.response.use((res: AxiosResponse) => {
	if (res.status !== 200) {
		ElMessage({
			message: `错误：${res.data.message}`,
			type: 'success',
		})
	}
	return res
})
auxiliary.interceptors.response.use((res: AxiosResponse) => {
	if (res.status !== 200) {
		ElMessage({
			message: `错误：${res.data.message}`,
			type: 'success',
		})
	}
	return res
})
user.interceptors.response.use((res: AxiosResponse) => {
	if (res.status !== 200) {
		ElMessage({
			message: `错误：${res.data.message}`,
			type: 'success',
		})
	}
	return res
})

const getRequest = (base_url: string, timeout: number) => {
	const req = axios.create({
		baseURL: base_url, // api 的 base_url  development   production
		timeout: timeout, // 请求超时时间
	})
	req.interceptors.response.use((res: AxiosResponse) => {
		if (res.status !== 200) {
			ElMessage({
				message: `错误：${res.data.message}`,
				type: 'success',
			})
		}
		return res
	})
	return req
}

export { request }
export { auxiliary }
export { user }
export { infrastructure }
export { generic }
export { updateRequest }
export { repairRequest }
export { purchaseRequest }
export { getRequest }
